package client;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.logging.Level;
import java.util.logging.Logger;

import message.Order;

import clientgui.MainWindow;

public class Client {

	private ObjectInputStream in = null;
	private ObjectOutputStream out = null;

	static public HashMap<String, Order> orders = new HashMap<>();
	static private int currentOrderIdNumber = 0;
	static public MainWindow mainwindow;

	static public BlockingQueue<Order> outQueue = new LinkedBlockingDeque<>();

	public static String getOrderId() {
		currentOrderIdNumber++;
		return "Order"
				+ ((currentOrderIdNumber > 9) ? currentOrderIdNumber : "0"
						+ currentOrderIdNumber);
	}

	public static Order getOrderById(String orderId) {
		return orders.get(orderId);
	}

	public static void registerOrder(Order o) {
		orders.put(o.getOrderId(), o);
	}

	public ObjectInputStream getIn() {
		return in;
	}

	public void setIn(ObjectInputStream in) {
		this.in = in;
	}

	public ObjectOutputStream getOut() {
		return out;
	}

	public void setOut(ObjectOutputStream out) {
		this.out = out;
	}

	public static void main(String[] args) {
		Logger log = Logger.getLogger("log.log");
		log.log(Level.INFO, "Client is starting ...");
		// ValidatorGUI val = new ValidatorGUI();
		MainWindow.startClientGUI();

		log.log(Level.INFO, "Main thread quit");
	}

	public static void doConnect(String clientName) {
		new Thread(new Connector(clientName)).start();
	}

}
